package com.easylinkin.linkappapi.hcmy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easylinkin.linkappapi.common.service.CommonService;
import com.easylinkin.linkappapi.hcmy.entity.AquacultureArea;
import com.easylinkin.linkappapi.hcmy.entity.AquacultureSpace;
import com.easylinkin.linkappapi.hcmy.mapper.AquacultureAreaMapper;
import com.easylinkin.linkappapi.hcmy.mapper.AquacultureSpaceMapper;
import com.easylinkin.linkappapi.hcmy.service.AquacultureService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * class info :
 *
 * @author liuqihang
 * @date 2021/10/18 15:07
 */
@Slf4j
@Service
public class AquacultureServiceImpl implements AquacultureService {

    @Resource
    private AquacultureSpaceMapper aquacultureSpaceMapper;

    @Resource
    private AquacultureAreaMapper aquacultureAreaMapper;

    @Resource
    private CommonService commonService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String savaAquaculture(AquacultureSpace aquacultureSpace) {
        Assert.notNull(aquacultureSpace.getStatisticYear(), "统计年度不能为空");
        Assert.notNull(aquacultureSpace.getSpaceId(), "示范区不能为空");

        QueryWrapper qw = new QueryWrapper();
        qw.eq("statistic_year", aquacultureSpace.getStatisticYear());
        qw.eq("space_id", aquacultureSpace.getSpaceId());
        List list = aquacultureSpaceMapper.selectList(qw);
        if(ObjectUtils.isNotEmpty(list)){
            return "统计年度和示范区数据重复";
        }
        commonService.setCreateAndModifyInfo(aquacultureSpace);
        commonService.setTenantIdInfo(aquacultureSpace);
        aquacultureSpaceMapper.insert(aquacultureSpace);

        if(ObjectUtils.isNotEmpty(aquacultureSpace.getAquacultureAreaList())){
            List<AquacultureArea> aquacultureAreaList = aquacultureSpace.getAquacultureAreaList();
            List<String> areaIdList = aquacultureAreaList.stream().map(AquacultureArea::getAreaId).collect(Collectors.toList());
            Set<String> areaIdSet = areaIdList.stream().collect(Collectors.toSet());
            if(areaIdSet.size() != areaIdList.size()){
                return "示范村数据不能重复";
            }
            for (AquacultureArea aquacultureArea:
                    aquacultureAreaList) {
                Assert.notNull(aquacultureArea.getAreaId(), "示范村不能为空");
                aquacultureArea.setAsId(aquacultureSpace.getId());
            }
            aquacultureAreaMapper.batchInsert(aquacultureAreaList);
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateAquaculture(AquacultureSpace aquacultureSpace) {
        Assert.notNull(aquacultureSpace.getId(), "ID不能为空");
        Assert.notNull(aquacultureSpace.getStatisticYear(), "统计年度不能为空");
        Assert.notNull(aquacultureSpace.getSpaceId(), "示范区不能为空");
        QueryWrapper qw2 = new QueryWrapper();
        qw2.notIn("id", aquacultureSpace.getId());
        qw2.eq("statistic_year", aquacultureSpace.getStatisticYear());
        qw2.eq("space_id", aquacultureSpace.getSpaceId());
        List list = aquacultureSpaceMapper.selectList(qw2);
        if(ObjectUtils.isNotEmpty(list)){
            return "统计年度和示范区数据重复";
        }

        AquacultureSpace aquacultureSpace1 = aquacultureSpaceMapper.selectById(aquacultureSpace.getId());
        if(aquacultureSpace1 == null){
            return "无此数据";
        }

        commonService.setModifyInfo(aquacultureSpace);
        aquacultureSpaceMapper.updateById(aquacultureSpace);

        if(ObjectUtils.isNotEmpty(aquacultureSpace.getAquacultureAreaList())){
            List<AquacultureArea> aquacultureAreaList = aquacultureSpace.getAquacultureAreaList();
            List<String> areaIdList = aquacultureAreaList.stream().map(AquacultureArea::getAreaId).collect(Collectors.toList());
            Set<String> areaIdSet = areaIdList.stream().collect(Collectors.toSet());
            if(areaIdSet.size() != areaIdList.size()){
                return "示范村数据不能重复";
            }

            QueryWrapper qw = new QueryWrapper();
            qw.eq("as_id", aquacultureSpace.getId());
            aquacultureAreaMapper.delete(qw);

            for (AquacultureArea aquacultureArea:
                    aquacultureAreaList) {
                Assert.notNull(aquacultureArea.getAreaId(), "示范村不能为空");
                aquacultureArea.setAsId(aquacultureSpace.getId());
            }
            aquacultureAreaMapper.batchInsert(aquacultureAreaList);
        }else {
            QueryWrapper qw = new QueryWrapper();
            qw.eq("as_id", aquacultureSpace.getId());
            aquacultureAreaMapper.delete(qw);
        }
        return null;
    }

    @Override
    public void deleteAquaculture(AquacultureSpace aquacultureSpace) {
        Assert.notNull(aquacultureSpace.getBatchId(), "批量删除ID参数不能为空");
        String[] split = aquacultureSpace.getBatchId().split(",");
        int[] array = Arrays.asList(split).stream().mapToInt(Integer::parseInt).toArray();
        if(array.length == 1){
            aquacultureSpaceMapper.deleteById(array[0]);
        }else {
            List<Integer> idList = Arrays.stream(array).boxed().collect(Collectors.toList());
            aquacultureSpaceMapper.deleteBatchIds(idList);
        }

        List<Integer> collect = Arrays.stream(array).boxed().collect(Collectors.toList());
        QueryWrapper qw = new QueryWrapper();
        qw.in("as_id", collect);
        aquacultureAreaMapper.delete(qw);

    }

    @Override
    public IPage<AquacultureSpace> getAquaculturePage(Page page, AquacultureSpace aquacultureSpace) {
        commonService.setTenantIdInfo(aquacultureSpace);
        return aquacultureAreaMapper.getAquacultureSpacePage(page, aquacultureSpace);
    }
}
